1 /** 2 The following examples come from 3 $(LINK http://zetcode.com/db/sqlite/constraints/). 4 Even though it is a SQLite tutorial the point is to show how to use this package 5 which does not have to be just SQLite. 6 */ 7 module test.examples_default; 8 9 version(D_Ddoc) 10 { 11 /// 12 class BlankClassSoDocsWillBeGenerated { } 13 } 14 15 16 /** 17 This example is for the DEFAULT constraint. The table 18 in SQL can be created by 19 $(D $(D $(D sql 20 CREATE TABLE Hotels 21 ( 22 Id INTEGER NOT NULL PRIMARY KEY, 23 Name TEXT, 24 City TEXT DEFAULT 'not available' 25 ); 26 27 ))) 28 29 Default in this package was made for foreign key interactions. With that 30 in mind I know in this example it seems like I should just set 31 the default value in the setter but then my package would not know what 32 default you want for a foreign key update rule or delete rule. 33 */ 34 unittest 35 { 36 import db_constraints; 37 38 class Hotel 39 { 40 private int _Id; 41 // marking Id with not null and primary key 42 @NotNull @PrimaryKeyColumn 43 @property int Id() 44 { 45 return _Id; 46 } 47 @property void Id(int value) 48 { 49 setter(_Id, value); 50 } 51 52 53 private string _Name; 54 @property string Name() 55 { 56 return _Name; 57 } 58 @property void Name(string value) 59 { 60 setter(_Name, value); 61 } 62 63 private string _City; 64 // marking city with its default 65 @Default!("not available") 66 @property string City() 67 { 68 return _City; 69 } 70 @property void City(string value) 71 { 72 setter(_City, value); 73 } 74 75 this(int Id_, string Name_, string City_) 76 { 77 this._Id = Id_; 78 this._Name = Name_; 79 this._City = City_; 80 initializeKeyedItem(); 81 } 82 this(int Id_, string Name_) 83 { 84 this._Id = Id_; 85 this._Name = Name_; 86 // using GetDefault will get the value you 87 // placed in the Default attribute 88 this._City = GetDefault!(Hotel, "City"); 89 initializeKeyedItem(); 90 } 91 92 mixin KeyedItem!(); 93 } 94 95 // make sure Hotel.City has a default attribute 96 assert(hasDefault!(Hotel, "City")); 97 // the default attribute has value not available 98 assert(GetDefault!(Hotel, "City") == "not available"); 99 100 auto i = new Hotel(1, "Kyjev", "Bratislava"); 101 assert(i.City == "Bratislava"); 102 // since City is not included in this constructor we use the default 103 auto j = new Hotel(2, "Slovan"); 104 assert(j.City == "not available"); 105 }